<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气推送信息管理</title>
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --success-color: #4cc9f0;
            --danger-color: #f72585;
            --warning-color: #f8961e;
            --light-color: #f8f9fa;
            --dark-color: #212529;
            --border-color: #dee2e6;
            --background-color: #f0f2f5;
            --card-bg: #ffffff;
            --text-primary: #333333;
            --text-secondary: #6c757d;
            --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            color: var(--text-primary);
        }

        .container {
            width: 100%;
            max-width: 500px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: white;
            font-size: 28px;
            font-weight: 600;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .header p {
            color: rgba(255, 255, 255, 0.85);
            margin-top: 8px;
            font-size: 14px;
        }

        .form-card {
            background: var(--card-bg);
            border-radius: 16px;
            padding: 30px;
            box-shadow: var(--shadow);
            transition: var(--transition);
        }

        .form-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--text-primary);
            font-size: 14px;
        }

        .required {
            color: var(--danger-color);
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid var(--border-color);
            border-radius: 10px;
            font-size: 15px;
            transition: var(--transition);
            background-color: #fafafa;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.15);
            background-color: white;
        }

        .btn {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: var(--transition);
            box-shadow: 0 4px 6px rgba(67, 97, 238, 0.2);
            margin-bottom: 10px;
        }

        .btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 6px 12px rgba(67, 97, 238, 0.3);
        }

        .btn:active {
            transform: translateY(0);
        }

        .btn-secondary {
            background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
            box-shadow: 0 4px 6px rgba(108, 117, 125, 0.2);
        }

        .btn-secondary:hover {
            box-shadow: 0 6px 12px rgba(108, 117, 125, 0.3);
        }

        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 10px;
            text-align: center;
            font-weight: 500;
            display: none;
        }

        .result.success {
            background-color: rgba(76, 201, 240, 0.15);
            color: #0a9396;
            border: 1px solid rgba(76, 201, 240, 0.3);
            display: block;
        }

        .result.error {
            background-color: rgba(247, 37, 133, 0.15);
            color: #b41950;
            border: 1px solid rgba(247, 37, 133, 0.3);
            display: block;
        }

        .form-row {
            display: flex;
            gap: 15px;
        }

        .form-col {
            flex: 1;
        }

        /* 添加提示图标样式 */
        .tooltip {
            position: relative;
            display: inline-block;
            margin-left: 5px;
            cursor: help;
        }

        .tooltip .tooltiptext {
            visibility: hidden;
            width: 300px;
            background-color: #333;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 10px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            transform: translateX(-50%);
            opacity: 0;
            transition: opacity 0.3s;
            font-size: 12px;
            line-height: 1.4;
        }

        .tooltip .tooltiptext::after {
            content: "";
            position: absolute;
            top: 100%;
            left: 50%;
            margin-left: -5px;
            border-width: 5px;
            border-style: solid;
            border-color: #333 transparent transparent transparent;
        }

        .tooltip:hover .tooltiptext {
            visibility: visible;
            opacity: 1;
        }

        .tooltip-icon {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 16px;
            height: 16px;
            background-color: var(--primary-color);
            color: white;
            border-radius: 50%;
            font-size: 10px;
            font-weight: bold;
        }

        /* 预览模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            animation: fadeIn 0.3s;
        }

        .modal-content {
            background-color: #fff;
            margin: 5% auto;
            padding: 0;
            border-radius: 16px;
            width: 90%;
            max-width: 400px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
            animation: slideIn 0.3s;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        @keyframes slideIn {
            from { transform: translateY(-50px); opacity: 0; }
            to { transform: translateY(0); opacity: 1; }
        }

        .modal-header {
            padding: 20px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal-header h2 {
            margin: 0;
            color: var(--text-primary);
            font-size: 20px;
        }

        .close {
            color: #aaa;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            line-height: 1;
        }

        .close:hover {
            color: var(--danger-color);
        }

        .modal-body {
            padding: 20px;
        }

        /* 实际推送效果预览样式 */
        .preview-container {
            max-width: 600px;
            margin: 0 auto;
            background: rgba(255,255,255,0.98);
            border-radius: 15px;
            box-shadow: 0 8px 30px rgba(0,0,0,0.12);
            font-family: "Segoe UI", Tahoma, sans-serif;
        }

        .preview-header {
            padding: 25px;
            background: linear-gradient(135deg, #6da2ff 0%, #88ccff 100%);
            color: white;
            border-radius: 15px 15px 0 0;
        }

        .preview-header-content {
            display: flex;
            align-items: center;
            gap: 60px;
        }

        .preview-weather-icon {
            width: 70px;
            height: 70px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 30px;
        }

        .preview-weather-info {
            flex: 1;
        }

        .preview-weather-temp {
            font-size: 36px;
            font-weight: bold;
            margin: 0 0 5px 0;
        }

        .preview-weather-desc {
            font-size: 18px;
            margin: 0;
        }

        .preview-info-grid {
            padding: 20px 25px;
            background: #f8f9fa;
            border-bottom: 1px solid #eee;
        }

        .preview-info-row {
            display: flex;
            gap: 20px;
        }

        .preview-info-item {
            flex: 1;
            text-align: center;
        }

        .preview-info-item:not(:first-child) {
            border-left: 1px solid #ddd;
            padding-left: 20px;
        }

        .preview-info-value {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .preview-info-label {
            font-size: 10px;
            color: #666;
        }

        .preview-countdown {
            padding: 20px 25px 0;
            display: flex;
            gap: 15px;
        }

        .preview-countdown-item {
            flex: 1;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
        }

        .preview-birthday {
            background: #fff3cd;
        }

        .preview-love {
            background: #d4edda;
        }

        .preview-countdown-label {
            font-size: 14px;
            margin: 0 0 8px 0;
        }

        .preview-countdown-value {
            font-size: 18px;
            font-weight: bold;
        }

        .preview-birthday .preview-countdown-value {
            color: #d39e00;
        }

        .preview-love .preview-countdown-value {
            color: #28a745;
        }

        .preview-advice {
            padding: 0 25px 25px;
            margin-top: 5px;
        }

        .preview-advice-item {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 15px;
            font-size: 14px;
            line-height: 1.5;
        }

        .preview-dressing-advice {
            color: #3a7bd5;
        }

        .preview-umbrella-advice {
            color: #28a745;
        }

        .modal-footer {
            padding: 15px 20px;
            border-top: 1px solid #eee;
            text-align: right;
        }

        /* 加载指示器 */
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
            margin-left: 10px;
            vertical-align: middle;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 10px;
            }
            
            .form-card {
                padding: 20px;
            }
            
            .header h1 {
                font-size: 24px;
            }
            
            .form-row {
                flex-direction: column;
                gap: 0;
            }
            
            .tooltip .tooltiptext {
                width: 250px;
                left: 0;
                transform: none;
            }
            
            .modal-content {
                margin: 10% auto;
                width: 95%;
            }
            
            .preview-header-content {
                gap: 20px;
            }
            
            .preview-info-row {
                flex-direction: column;
                gap: 10px;
            }
            
            .preview-info-item:not(:first-child) {
                border-left: none;
                border-top: 1px solid #ddd;
                padding: 10px 0 0 0;
            }
            
            .preview-countdown {
                flex-direction: column;
                gap: 10px;
            }
        }
        
        /* PC端优化 */
        @media (min-width: 769px) {
            .container {
                max-width: 600px;
            }
            
            .form-card {
                padding: 40px;
            }
            
            .header h1 {
                font-size: 32px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🌤️ 天气推送管理</h1>
            <p>设置您的个性化天气推送信息</p>
        </div>
        
        <div class="form-card">
            <form id="dataForm">
                <div class="form-group">
                    <label for="appToken">Wx-appToken <span class="required">*</span>
                        <div class="tooltip">
                            <span class="tooltip-icon">?</span>
                            <span class="tooltiptext">获取AppToken的文档地址：<br>https://wxpusher.zjiecode.com/docs/#/?id=%e8%8e%b7%e5%8f%96apptoken<br>点击可查看详细说明</span>
                        </div>
                    </label>
                    <input type="text" id="appToken" class="form-control" required placeholder="请输入您的appToken">
                </div>
                
                <div class="form-group">
                    <label for="area">地区 <span class="required">*</span>
                        <div class="tooltip">
                            <span class="tooltip-icon">?</span>
                            <span class="tooltiptext">请输入完整的地区名称，如：北京市、上海市浦东新区、广东省深圳市南山区等</span>
                        </div>
                    </label>
                    <input type="text" id="area" class="form-control" required placeholder="请输入地区名称，如：北京市">
                </div>

                <div class="form-group">
                    <label for="mobile">手机号 <span class="required">*</span></label>
                    <input type="tel" id="mobile" class="form-control" required pattern="[0-9]{11}" placeholder="请输入11位手机号">
                </div>

                <div class="form-row">
                    <div class="form-col">
                        <div class="form-group">
                            <label for="birthday">生日 <span class="required">*</span></label>
                            <input type="date" id="birthday" class="form-control" required max="">
                        </div>
                    </div>
                    <div class="form-col">
                        <div class="form-group">
                            <label for="loveDay">恋爱纪念日 <span class="required">*</span></label>
                            <input type="date" id="loveDay" class="form-control" required max="">
                        </div>
                    </div>
                </div>
                
                <button type="button" id="previewBtn" class="btn btn-secondary">📱 预览推送效果</button>
                <button type="submit" class="btn">💾 保存推送设置</button>
            </form>
            
            <div id="result" class="result"></div>
        </div>
    </div>

    <!-- 预览模态框 -->
    <div id="previewModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2>推送效果预览</h2>
                <span class="close">&times;</span>
            </div>
            <div class="modal-body">
                <div class="preview-container">
                    <div class="preview-header">
                        <div class="preview-header-content">
                            <div class="preview-weather-icon">☀️</div>
                            <div class="preview-weather-info">
                                <h1 class="preview-weather-temp">20°C</h1>
                                <p class="preview-weather-desc">晴天</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="preview-info-grid">
                        <div class="preview-info-row">
                            <div class="preview-info-item">
                                <div class="preview-info-value" id="previewMaxTemp">25°C</div>
                                <div class="preview-info-label">最高温度</div>
                            </div>
                            <div class="preview-info-item">
                                <div class="preview-info-value" id="previewMinTemp">15°C</div>
                                <div class="preview-info-label">最低温度</div>
                            </div>
                            <div class="preview-info-item">
                                <div class="preview-info-value" id="previewWind">3级</div>
                                <div class="preview-info-label" id="previewWindDir">东南风</div>
                            </div>
                            <div class="preview-info-item">
                                <div class="preview-info-value" id="previewHumidity">65%</div>
                                <div class="preview-info-label">当前湿度</div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="preview-countdown">
                        <div class="preview-countdown-item preview-birthday">
                            <div class="preview-countdown-label">🎂 生日倒计时：</div>
                            <div class="preview-countdown-value" id="previewBirthdayCount">还剩100天</div>
                        </div>
                        <div class="preview-countdown-item preview-love">
                            <div class="preview-countdown-label">❤️ 恋爱天数：</div>
                            <div class="preview-countdown-value" id="previewLoveCount">520天</div>
                        </div>
                    </div>
                    
                    <div class="preview-advice">
                        <div class="preview-advice-item preview-dressing-advice" id="previewDressingAdvice">
                            🌤️ 温度适宜（15°C~25°C），薄外套+长袖衬衫最佳选择
                        </div>
                        <div class="preview-advice-item preview-umbrella-advice" id="previewUmbrellaAdvice">
                            🌤️ 今日无雨（降水概率20%），可以放心出门
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button id="closePreview" class="btn btn-secondary">关闭</button>
            </div>
        </div>
    </div>

    <script>
        // 初始化日期控件
        const today = new Date().toISOString().split('T')[0];
        document.getElementById('birthday').setAttribute('max', today);
        document.getElementById('loveDay').setAttribute('max', today);
        
        // 页面加载完成后初始化功能
        window.addEventListener('DOMContentLoaded', async () => {
            initPreviewFunctionality();
        });
        
        // 初始化预览功能
        function initPreviewFunctionality() {
            // 预览按钮事件
            document.getElementById('previewBtn').addEventListener('click', showPreview);
            
            // 模态框关闭事件
            document.querySelector('#previewModal .close').addEventListener('click', closePreview);
            document.getElementById('closePreview').addEventListener('click', closePreview);
            
            // 点击模态框外部关闭
            window.addEventListener('click', function(event) {
                const modal = document.getElementById('previewModal');
                if (event.target === modal) {
                    closePreview();
                }
            });
        }
        
        // 显示预览
        function showPreview() {
            // 获取表单数据
            const appToken = document.getElementById('appToken').value;
            const area = document.getElementById('area').value || '未填写';
            const mobile = document.getElementById('mobile').value || '未填写';
            const birthday = document.getElementById('birthday').value || '未填写';
            const loveDay = document.getElementById('loveDay').value || '未填写';
            
            // 验证必填项
            if (!appToken || !area || !mobile || !birthday || !loveDay) {
                showMessage('请填写所有必填项后再预览', 'error');
                return;
            }
            
            // 计算倒计时天数
            const birthdayCount = calculateBirthdayCount(birthday);
            const loveCount = calculateLoveDays(loveDay);
            
            // 更新预览内容
            document.getElementById('previewMaxTemp').textContent = '25°C';
            document.getElementById('previewMinTemp').textContent = '15°C';
            document.getElementById('previewWind').textContent = '3级';
            document.getElementById('previewWindDir').textContent = '东南风';
            document.getElementById('previewHumidity').textContent = '65%';
            document.getElementById('previewBirthdayCount').innerHTML = `还剩${birthdayCount}天`;
            document.getElementById('previewLoveCount').textContent = `${loveCount}天`;
            document.getElementById('previewDressingAdvice').textContent = '🌤️ 温度适宜（15°C~25°C），薄外套+长袖衬衫最佳选择';
            document.getElementById('previewUmbrellaAdvice').textContent = '🌤️ 今日无雨（降水概率20%），可以放心出门';
            
            // 显示模态框
            document.getElementById('previewModal').style.display = 'block';
        }
        
        // 计算生日倒计时天数
        function calculateBirthdayCount(birthday) {
            if (!birthday) return 100;
            const birthdayDate = new Date(birthday);
            const today = new Date();
            const thisYearBirthday = new Date(today.getFullYear(), birthdayDate.getMonth(), birthdayDate.getDate());
            
            // 如果今年的生日已过，则计算明年的生日
            if (thisYearBirthday < today) {
                thisYearBirthday.setFullYear(today.getFullYear() + 1);
            }
            
            const diffTime = thisYearBirthday - today;
            return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
        }
        
        // 计算恋爱天数
        function calculateLoveDays(loveDay) {
            if (!loveDay) return 520;
            const loveDate = new Date(loveDay);
            const today = new Date();
            const diffTime = today - loveDate;
            return Math.floor(diffTime / (1000 * 60 * 60 * 24));
        }
        
        // 关闭预览
        function closePreview() {
            document.getElementById('previewModal').style.display = 'none';
        }
        
        // 显示消息
        function showMessage(message, type) {
            const resultDiv = document.getElementById('result');
            resultDiv.textContent = message;
            resultDiv.className = `result ${type}`;
            
            // 3秒后自动隐藏成功消息
            if (type === 'success') {
                setTimeout(() => {
                    resultDiv.style.display = 'none';
                }, 3000);
            }
        }
        
        // 表单提交处理
        document.getElementById('dataForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            // 获取表单数据
            const appToken = document.getElementById('appToken').value;
            const area = document.getElementById('area').value;
            const mobile = document.getElementById('mobile').value;
            const birthday = document.getElementById('birthday').value;
            const loveDay = document.getElementById('loveDay').value;
            
            // 验证必填项
            if (!appToken || !area || !mobile || !birthday || !loveDay) {
                showMessage('请填写所有必填项', 'error');
                return;
            }
            
            // 验证日期范围
            if (birthday > today || loveDay > today) {
                showMessage('生日和恋爱纪念日不能晚于当前日期', 'error');
                return;
            }
            
            const resultDiv = document.getElementById('result');
            resultDiv.textContent = '处理中...';
            resultDiv.className = 'result';
            resultDiv.style.display = 'block';
            
            try {
                const response = await fetch('/v1/api/cron/savePushInfo', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        appToken: appToken,
                        areaCode: area, // 使用地区名称作为参数
                        mobile: mobile,
                        birthday: birthday,
                        loveDay: loveDay
                    })
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP错误! 状态码: ${response.status}`);
                }
                
                const message = await response.text();
                showMessage(`✅ ${message}`, 'success');
                
                // 清空表单
                document.getElementById('dataForm').reset();
            } catch (error) {
                showMessage(`❌ 操作失败: ${error.message}`, 'error');
            }
        });
    </script>
</body>
</html>